home *** CD-ROM | disk | FTP | other *** search
- {*********************************************************}
- {* AAPSPrcs *}
- {* Copyright (c) Julian M Bucknall 1998-1999 *}
- {* All rights reserved. *}
- {*********************************************************}
- {* Algorithms Alfresco postscript routines *}
- {*********************************************************}
-
- {Note: this unit is released as freeware. In other words, you are free
- to use this unit in your own applications, however I retain all
- copyright to the code. JMB}
-
- {Warning: this unit is very much a work in progress. It will be
- changing often as I build up a set of routines (maybe even
- classes) to create EPS files. At present, this unit is nothing
- more than a set of experimental routines. JMB}
-
- unit PSProcs;
-
- interface
-
- uses
- SysUtils,
- Classes;
-
- const
- AAPSArrowHeight = 9;
- AAPSArrowWidth = 2;
-
- type
- TaaPSPoint = packed record
- X, Y : integer;
- end;
- TaaPSPath = array [0..99] of TaaPSPoint; {!! 99 is arbitrary}
-
- TaaPSIndexes = array [1..10] of integer;
-
- procedure AAPSOutputProlog(SList : TStrings);
- procedure AAPSOutputEpilog(SList: TStrings);
-
- procedure AAPSDrawLine(SList: TStrings; FromX, FromY, ToX, ToY : integer);
- procedure AAPSDrawSquare(SList: TStrings; aX, aY, aWidth : integer);
- procedure AAPSDrawRect(SList: TStrings; aX, aY, aWidth, aHeight : integer);
- procedure AAPSDrawRectFill(SList: TStrings; aX, aY, aWidth, aHeight : integer;
- aGray : single);
- procedure AAPSTracePath(SList: TStrings; const aPath : TaaPSPath; aCount : integer);
- procedure AAPSDrawPath(SList: TStrings; const aPath : TaaPSPath; aCount : integer);
- procedure AAPSDrawPathFill(SList: TStrings; const aPath : TaaPSPath; aCount : integer);
- procedure AAPSDrawText(SList: TStrings; aSt : string; aX, aY, aPoint : integer);
- procedure AAPSDrawArrow(SList: TStrings; aPath : TaaPSPath);
- procedure AAPSDrawTextInBox(SList: TStrings; aSt : string; aX, aY, aPoint : integer;
- aIndexes : TaaPSIndexes; aInxSt : string);
-
- implementation
-
- procedure AAPSOutputProlog(SList : TStrings);
- begin
- with SList do begin
- Add('%!PS-Adobe-3.0 EPSF-3.0');
- Add('%%BoundingBox: 0 0 450 720'); {!! should calculate the size}
- Add('%%Pages: 1');
- Add('gsave');
- end;
- end;
-
- procedure AAPSOutputEpilog(SList: TStrings);
- begin
- with SList do begin
- Add('showpage');
- Add('grestore');
- end;
- end;
-
- procedure AAPSDrawRectPrim(SList: TStrings; aX, aY, aWidth, aHeight : integer);
- begin
- with SList do begin
- Add(Format('%% draw a rect at (%d, %d) with width %d, height %d ',
- [aX, aY, aWidth, aHeight]));
- Add('newpath');
- Add(Format(' %d %d moveto', [aX, aY]));
- Add(Format(' %d 0 rlineto', [aWidth]));
- Add(Format(' 0 %d rlineto', [aHeight]));
- Add(Format(' -%d 0 rlineto', [aWidth]));
- Add('closepath');
- end;
- end;
-
-
- procedure AAPSDrawRect(SList: TStrings; aX, aY, aWidth, aHeight : integer);
- begin
- AAPSDrawRectPrim(SList, aX, aY, aWidth, aHeight);
- with SList do begin
- Add('stroke');
- end;
- end;
-
- procedure AAPSDrawRectFill(SList: TStrings; aX, aY, aWidth, aHeight : integer;
- aGray : single);
- begin
- AAPSDrawRectPrim(SList, aX, aY, aWidth, aHeight);
- with SList do begin
- Add('gsave');
- Add(Format(' %.2f setgray', [aGray]));
- Add('fill');
- Add('grestore');
- Add('stroke');
- end;
- end;
-
- procedure AAPSDrawSquare(SList: TStrings; aX, aY, aWidth : integer);
- begin
- AAPSDrawRect(SList, aX, aY, aWidth, aWidth);
- end;
-
- procedure AAPSDrawLine(SList: TStrings; FromX, FromY, ToX, ToY : integer);
- begin
- with SList do begin
- Add('%% draw a line');
- Add(Format('%d %d moveto', [FromX, FromY]));
- Add(Format('%d %d lineto', [ToX, ToY]));
- Add('stroke');
- end;
- end;
-
- procedure AAPSTracePath(SList: TStrings; const aPath : TaaPSPath; aCount : integer);
- var
- i : integer;
- begin
- with SList do begin
- Add('%% trace a path');
- Add('newpath');
- Add(Format('%d %d moveto', [aPath[0].X, aPath[0].Y]));
- for i := 1 to pred(aCount) do begin
- Add(Format('%d %d lineto', [aPath[i].X, aPath[i].Y]));
- end;
- Add('closepath');
- end;
- end;
-
- procedure AAPSDrawPath(SList: TStrings; const aPath : TaaPSPath; aCount : integer);
- begin
- with SList do begin
- Add('%% draw a path');
- AAPSTracePath(SList, aPath, aCount);
- Add('stroke');
- end;
- end;
-
- procedure AAPSDrawPathFill(SList: TStrings; const aPath : TaaPSPath; aCount : integer);
- begin
- with SList do begin
- Add('%% draw a path and fill');
- AAPSTracePath(SList, aPath, aCount);
- Add('fill');
- end;
- end;
-
- procedure AAPSDrawText(SList: TStrings; aSt : string; aX, aY, aPoint : integer);
- begin
- with SList do begin
- Add(Format('%% draw text at (%d, %d) with point size %d', [aX, aY, aPoint]));
- Add(Format('%d %d moveto', [aX, aY]));
- Add(Format('/Helvetica findfont %d scalefont setfont', [aPoint]));
- Add(Format('(%s) show', [aSt]));
- end;
- end;
-
- procedure AAPSDrawArrow(SList: TStrings; aPath : TaaPSPath);
- var
- Path : TaaPSPath;
- Sign : integer;
- Theta : double;
- aX, aY, aToX, aToY : integer;
- begin
- aX := aPath[0].X;
- aY := aPath[0].Y;
- aToX := aPath[1].X;
- aToY := aPath[1].Y;
- if (aToX = aX) then begin
- {vertical}
- if (aToY > aY) then
- Sign := 1
- else
- Sign := -1;
- Path[0].X := aX;
- Path[0].Y := aToY - (Sign * AAPSArrowHeight);
- Path[1].X := aX - AAPSArrowWidth;
- Path[1].Y := Path[0].Y;
- Path[2].X := aToX;
- Path[2].Y := aToY;
- Path[3].X := aX + AAPSArrowWidth;
- Path[3].Y := Path[0].Y;
- end
- else begin
- {other angle}
- Theta := arctan((aToY - aY) / (aToX - aX));
- if (aToX > aX) then
- Sign := 1
- else
- Sign := -1;
- Path[0].X := Round(aToX - Sign * (AAPSArrowHeight * cos(Theta)));
- Path[0].Y := Round(aToY - Sign * (AAPSArrowHeight * sin(Theta)));
- Path[1].X := Round(Path[0].X - Sign * (AAPSArrowWidth * sin(Theta)));
- Path[1].Y := Round(Path[0].Y + Sign * (AAPSArrowWidth * cos(Theta)));
- Path[2].X := aToX;
- Path[2].Y := aToY;
- Path[3].X := Round(Path[0].X + Sign * (AAPSArrowWidth * sin(Theta)));
- Path[3].Y := Round(Path[0].Y - Sign * (AAPSArrowWidth * cos(Theta)));
- end;
- with SList do begin
- Add(Format('%d %d moveto', [aX, aY]));
- Add(Format('%d %d lineto', [Path[0].X, Path[0].Y]));
- Add('stroke');
- AAPSDrawPathFill(SList, Path, 4);
- end;
- end;
-
-
- procedure AAPSDrawTextInBox(SList: TStrings; aSt : string; aX, aY, aPoint : integer;
- aIndexes : TaaPSIndexes; aInxSt : string);
- var
- Width : integer;
- i : integer;
- BumpCenter : integer;
- X : integer;
- Arrow : TaaPSPath;
- begin
- {draw the boxes and text}
- Width := aPoint * 3 div 2;
- BumpCenter := (Width - aPoint);
- X := aX;
- for i := 1 to length(aSt) do begin
- AAPSDrawSquare(Slist, X, aY, Width);
- AAPSDrawText(SList, aSt[i], X + BumpCenter, aY + BumpCenter, aPoint);
- inc(X, Width);
- end;
- {draw the shadow}
- with SList do begin
- Add('gsave');
- Add(' 3 setlinewidth');
- Add(Format(' %d %d moveto', [aX+1, aY-1]));
- Add(Format(' %d 0 rlineto', [Width * length(aSt)]));
- Add(Format(' 0 %d rlineto', [Width]));
- Add(' stroke');
- Add('grestore');
- end;
- for i := 1 to length(aInxSt) do begin
- X := aX + (Width * (aIndexes[i] - 1));
- Arrow[0].X := X + (Width div 2);
- Arrow[0].Y := aY - (2 * AAPSArrowHeight);
- Arrow[1].X := Arrow[0].X;
- Arrow[1].Y := aY - 4;
- AAPSDrawArrow(SList, Arrow);
- AAPSDrawText(SList, aInxSt[i], X + BumpCenter, Arrow[0].Y - aPoint, aPoint);
- end;
- end;
-
- end.
-